Xbasic

SQL::ConnectionLastInsertedIdentity Method

Syntax

Result as A = LastInsertedIdentity([TableInfo as SQL::TableInfo])

Arguments

TableInfoSQL::TableInfo

A SQL::TableInfo object.

Returns

ResultAny Type

The last inserted identity, if available.

Description

Returns the last inserted identity for a specific table (where possible) after executing an INSERT statement.

Discussion

The LastInsertedIdentity() method retrieves the last identity value generated for an INSERT on the currently open connection.

  1. This is current connection specific for most databases. For databases that use sequences to maintain identity (such as Oracle), the sequence may not be correct after the transaction has been completed.

  2. For databases that use sequences to generate identities <TableInfo> is a required parameter. It must include an identity column in its definition. That column must specify the sequence name to be used.

Cn.CallResult.LastInsertedIdentity is always preferable to Cn.LastInsertedIdentity() after an INSERT is executed. *Cn.CallResult.LastInsertedIdentity will contain the identity for the record that was just inserted after executing an INSERT statement while the SQL::Connection LastInsertedIdentity() method returns the last inserted identity for the current open table.

See also SQL::GenerateLastInsertedIdentityStatement().

dim cn as sql::Connection
dim insert as c =<<%sql%
INSERT INTO employees
(FirstName,LastName,HireDate)
VALUES
(:Firstname,:Lastname,:HireDate);
%sql%

dim args as sql::Arguments
args.set("Firstname","Alma")
args.set("Lastname","Vasquez")
args.set("HireDate",date())

dim id as N = 0
if (cn.open("::Name::mysql_northwind")) then
    if (cn.execute(insert,args)) then
        ' insert success
        id = cn.LastInsertedIdentity()
    else
        ' insert failed
    end if
        cn.close()
else
    ' could not open connection 
end if

See Also